package br.com.lpoint.job.scheduler;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Stateless;
import javax.ejb.Timer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.commons.discovery.ResourceClass;
import org.apache.commons.discovery.ResourceClassIterator;
import org.apache.commons.discovery.ResourceIterator;
import org.apache.commons.discovery.ResourceNameIterator;
import org.apache.commons.discovery.resource.classes.DiscoverClasses;
import org.apache.commons.discovery.tools.DiscoverClass;
import org.apache.commons.discovery.tools.SPInterface;

import br.com.document.job.JobDocument;
import br.com.document.job.session.JobSession;
import br.com.document.job.session.LPointSessionFactory;
import br.com.document.manager.session.SessionConfig;
import br.com.lpoint.job.dao.JobDAO;

@Stateless
@Singleton
public class JobSchedulerImpl implements JobScheduler{
	
	@EJB(beanInterface=JobDAO.class)
	private JobDAO jobDAO;
	
	@Resource(mappedName="jms/QueueFactory")
	private  ConnectionFactory queueFactory;
	
	private JobSession jobSession;
	
	@Resource(mappedName="jms/QueueJob")
	private  Queue queue;
	
	private Connection queueConnection = null;
	private Session session = null;
	private MessageProducer producer = null;
	

    @Schedule(dayOfMonth = "*", month="*", year = "*", second = "*/10", minute="*", hour="7", info="verificaSeHaSolicitacoesASeremProcessadasTimer",persistent=false)
	public void getJobParaRodar(final Timer timer) {
		System.out.println("Executando o timer"+new Date());
		
		SessionConfig config = new SessionConfig();
		config.addParameter("user", null);
		config.addParameter("password", null);
		config.addParameter("schema", null);
		
		jobSession = (JobSession) LPointSessionFactory.getLPointSession(config);
		//jobDAO.hasJobParaRodar();
		//enviaMensagem("JobExample", new HashMap<String,Object>());
		jobSession.getAllJobs();
	}

	@Override
	public void enviaMensagem(String nomeDoJob, Map<String, Object> params) {
		try {
			queueConnection = queueFactory.createConnection();
			session = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			producer = session.createProducer(queue);
			
			MapMessage message = null;
			try {
				message = session.createMapMessage();
				message.setString("nome", "br.com.lpoint.job.test.JobExampleTest");
				message.setStringProperty("MessageFormat", "X");
				producer.send(message);
			}
			catch (Exception e){
				e.printStackTrace();
			}finally{
				producer.close();
				session.close();
				queueConnection.close();
			}	
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void verificaJobsPresentesNoServer(){
		try {
			ServiceLoader<JobDocument> loader = ServiceLoader.load(JobDocument.class);
			
			JobDocument iJob = null;
			for (JobDocument jobDocument : loader) {
				iJob = jobDocument;
				iJob.execute(null);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}